 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : CdutHelperAndroid
 * Package  : net.solosky.cduthelper.android.adapter
 * File     : FunctionMenuListAdapter.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2011-5-20
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.solosky.easyavatar.R;
import net.solosky.easyavatar.utils.Utils;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 *
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class FunctionMenuListAdapter extends SimpleListAdapter<FunctionMenu> {
	public FunctionMenuListAdapter(Context context, String sectionName) {
		super(context, new ArrayList<FunctionMenu>());
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(context.getResources().openRawResource(R.raw.menus));
			List sections = doc.getRootElement().getChildren();
			Iterator<Element> it = sections.iterator();
			while(it.hasNext()){
				Element e = it.next();
				if(e.getAttributeValue("name").equals(sectionName)){
					List<Element> menus = e.getChildren();
					Iterator<Element> mit = menus.iterator();
					while(mit.hasNext()){
						Element m = mit.next();
						int id = Integer.parseInt(m.getAttributeValue("id"));
						int icon = context.getResources().getIdentifier(m.getAttributeValue("icon"), "drawable", context.getPackageName());
						getDataList().add(new FunctionMenu(id, icon, m.getAttributeValue("title"), m.getAttributeValue("desc")));
					}
					break;
				}
			}
		} catch (Exception e) {
			Utils.dout(e);
		}
	}

	/* (non-Javadoc)
	 * @see net.solosky.cduthelper.android.adapter.SimpleListAdapter#newView(int)
	 */
	@Override
	public ViewHolder newView(int position) {
		View view = inflate(R.layout.li_func_menu);
		FuncViewHolder holder = new FuncViewHolder();
		holder.contentView = view;
		holder.img = (ImageView)view.findViewById(R.id.func_img);
		holder.title = (TextView)view.findViewById(R.id.func_title);
		holder.info = (TextView)view.findViewById(R.id.func_intro);
		return holder;
	}

	/* (non-Javadoc)
	 * @see net.solosky.cduthelper.android.adapter.SimpleListAdapter#fillView(net.solosky.cduthelper.android.adapter.SimpleListAdapter.ViewHolder, int)
	 */
	@Override
	public void fillView(ViewHolder viewHolder, int position) {
		FunctionMenu menu = (FunctionMenu) getItem(position);
		FuncViewHolder holder = (FuncViewHolder) viewHolder;
		holder.img.setBackgroundResource(menu.getIcon());
		holder.title.setText(menu.getTitle());
		holder.info.setText(menu.getDesc());
	}
	
	public final class FuncViewHolder extends ViewHolder{
		public ImageView img;
		public TextView title;
		public TextView info;
	}

}
